﻿function Get-TcpConnectionStatus()
{
    [CmdletBinding()]
		param (
			[parameter(Mandatory=$true)]
			[String]$serverName,
			[parameter(Mandatory=$true)]
			[Int32]$port,
			[parameter(Mandatory=$false)]
			[String]$Comment="Check Endpoint"
			)
    PROCESS 
	{
        $msg = [System.String]::Format("Check connection for {0} on {1}", $serverName, $port)
        Write-Verbose $msg
        
        $tcpClient = New-Object System.Net.Sockets.TCPClient

        $checkResult = New-Object System.Object
        $checkResult | Add-Member -type NoteProperty -name ServerName -value $serverName        
        $checkResult | Add-Member -type NoteProperty -name Port -value $port
        $checkResult | Add-Member -type NoteProperty -name Status -Value $null
        $checkResult | Add-Member -type NoteProperty -name LocalEndPoint -Value $null
        $checkResult | Add-Member -type NoteProperty -name RemoteEndPoint -Value $null
		$checkResult | Add-Member -type NoteProperty -name Comment -Value $Comment
        #$checkResult | Add-Member -type NoteProperty -name TTL -value -Value 0

        try
        {
            $tcpClient.Connect($serverName,$port)
            $checkResult.Status = $tcpClient.Connected
            $checkResult.LocalEndPoint = $tcpClient.Client.LocalEndPoint
            $checkResult.RemoteEndPoint = $tcpClient.Client.RemoteEndPoint
            #$checkResult.TTL = $tcpClient.Client.Ttl
        }
        catch
        {
            $checkResult.Status = $False
        }
        finally
        {
            
            write-output $checkResult
            $tcpClient.Close()
        }
        $msg = [System.String]::Format("End Check connection for {0} on {1}", $serverName, $port)
        Write-Verbose $msg        
    }
}    


function Get-DomainControllers()
{
    $hostEntry=New-Object -TypeName System.Net.IPHostEntry
    $configurationContainer = ([adsi] "LDAP://RootDSE").Get("ConfigurationNamingContext")
    $partitions = ([adsi] "LDAP://CN=Partitions,$configurationContainer").psbase.children
    
    #"Domain`tDomainController`tIPAddress" ¦ Out-File -FilePath "DomainControllers.txt"
    
    foreach($partition in $partitions)
    {
        if($partition.netbiosName -ne "")
        {
            #"DCs in the " + $partition.netbiosName + " Domain"
            $partitionDN=$partition.ncName
            $dcContainer=[adsi] "LDAP://ou=domain controllers,$partitionDN"
            $dcs = $dcContainer.psbase.children
            foreach($dc in $dcs)
            {
                $hostEntry= [System.Net.Dns]::GetHostByName($dc.dnsHostName)
                #"`t" + $dc.dnsHostName + "`t" + $hostEntry.AddressList[0].IPAddressToString
                #"$($partition.netbiosName)`t$($dc.dnsHostName)`t$($hostEntry.AddressList[0].IPAddressToString)" ¦ Out-File -FilePath "DomainControllers.txt" -Append
                $DCresult = New-Object System.Object
                $DCresult | Add-Member -type NoteProperty -name DnsHostName -value  $dc.dnsHostName.ToString()
                $DCresult | Add-Member -type NoteProperty -name NetbiosName -value  $partition.netbiosName.ToString()                      
                $DCresult | Add-Member -type NoteProperty -name IPAddress -value $hostEntry.AddressList[0].IPAddressToString                      
                $DCresult | Add-Member -type NoteProperty -name OperatingSystem -value $dc.OperatingSystem.ToString()
                $DCresult | Add-Member -type NoteProperty -name LastChanged -value $dc.WhenChanged.ToString()                                      
                Write-Output $DCresult
            }
        }
    }
}

